package com.yifan.diyFuncion.case2;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StopWatch;

import java.util.Map;
import java.util.function.Function;

/**
 * @description：TODO
 * @author：yufan.wang
 * @create：2024/11/8 11:33
 */
public class Case2FunUtil {

    /**
     * @param Function<Person,T> 接收Person对象，返回T
     * @return
     * @author yufan.wang
     * @date 2024/11/8 11:34
     */
    private static <T> T getBean(Function<Person, T> handlerFunc) {
        StopWatch watch = new StopWatch();
        watch.start();
        Person person = new Person();
        T apply = handlerFunc.apply(person);
        watch.stop();
        System.out.println("执行时间：" + watch.getTotalTimeMillis());
        return apply;
    }

    public static void main(String[] args) {
        //箭头左边输入参数：person1111 为输入参数
        //箭头右边输出结果：person1111.sayHi("zhangshan") 既返回的结果是执行sayHi方法的返回值
        String zhangshan = getBean(person1111 -> person1111.sayHi("zhangshan"));
        System.out.println(zhangshan); //输出：Hello World zhangshan
    }
}


class Person {
    public String sayHi(String param) {
        return "Hello World " + param;
    }
}



//实际应用 参考如下

//    springboot项目中可以用map接收注入的bean，key为beanName，value为bean实例

//    @Autowired
//    private Map<String, PosTransactionService> impMap;

//    private  <T> T getBean(String type, Function<PosTransactionService, T> handlerFunc){
//        PosTransactionService posTransactionService = impMap.get(getKey(type));
//        if (null==posTransactionService){
//            throw new BusinessException(PosUnknownExceptionInfo.UNKNOWN, "pay-adapter中交易无可用实现[" + type + "]");
//        }
//        StopWatch watch = new StopWatch();
//        watch.start();
//        T apply = handlerFunc.apply(posTransactionService);
//        watch.stop();
//        log.info("获取{}缴费适配模块响应，耗时{}, 内容\n: {}", posTransactionService.getServiceName(),watch.getTotalTimeMillis(), apply);
//        return apply;
//    }
//    private String getKey(String cardType) {
//        return cardType.toLowerCase() + "TypeTransactionService";
//    }

